package pool

import (
	"fmt"
	"sync"
	"time"
)

type buffer []byte

type A struct {
	Name string
	Age  int
	buf  buffer
}

func (a A) say() {
	fmt.Println(a.Name, "问好：", a.Age)
}

func (a A) free() {
	//为了测试第2个协成拿对象时，拿到一个new的新对象
	time.Sleep(time.Second * 5)
	a.Name = "default"
	a.Age = 0
	pool.Put(a)
}

var (
	pool = sync.Pool{
		New: func() interface{} {
			return new(A)
		},
	}
	buf = buffer{}
)

func OutLine(name string, age int) {
	model := newPrinter()
	model.say()
	fmt.Println(buf)
	model.Name = name
	model.Age = age
	model.say()
	fmt.Println(buf)
	go model.free()
}

func newPrinter() *A {
	p := pool.Get().(*A)
	p.Name = "default"
	p.Age = 0
	p.buf = []byte("sssss")
	buf = p.buf
	return p
}
